package com.alibaba.cloud.interfaces.web;

import java.time.LocalDateTime;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.cloud.application.UserInfoService;
import com.alibaba.cloud.application.mapping.UserInfoMapping;
import com.alibaba.cloud.domain.UserInfo;
import com.alibaba.cloud.domain.dto.UserInfoDto;
import com.alibaba.cloud.utils.ResultVo;

import jakarta.validation.Valid;
import jakarta.validation.groups.Default;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author cruise
 * @since 2022-06-13
 */
@RestController
@RequestMapping("/userInfo")
@RequiredArgsConstructor
public class UserInfoController {
    private final UserInfoService userInfoService;
    @GetMapping("userInfo")
    private ResultVo<UserInfo> userInfo(Long id) {
        return ResultVo.success(userInfoService.getById(id));
    }

    /***
     * @description: 可以触发参数校验
     * @date: 2022/3/29 22:52
     * @author: zhb1316783262@163.com
     **/
    @PostMapping("insert")
    private ResultVo<Boolean> insert(@Valid @RequestBody UserInfoDto userInfoDto) {
        UserInfo userInfo = UserInfoMapping.INSTANCE.dtoToPO(userInfoDto);
        userInfo.setCreateTime(LocalDateTime.now());
        return ResultVo.success(userInfoService.save(userInfo));
    }

    /***
     * @description: 依然可以触发参数校验
     * @date: 2022/3/29 22:52
     * @author: zhb1316783262@163.com
     **/
    @PostMapping("save")
    private ResultVo<Boolean> save(@Validated(Default.class) @RequestBody UserInfoDto userInfoDto) {
        UserInfo userInfo = UserInfoMapping.INSTANCE.dtoToPO(userInfoDto);
        userInfo.setCreateTime(LocalDateTime.now());
        return ResultVo.success(userInfoService.save(userInfo));
    }
}
